package ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import metier.Adherent;
import dao.DaoAdherent;

@SuppressWarnings("serial")
public class FenetreGestionMoniteur extends JFrame implements ActionListener, ItemListener
{
	private JPanel contenu;
	private JMenuBar menuBar;
	private JMenu mnFichier;
	private JMenuItem mniQuitter;
	private JMenu mnGestion;
	private JMenuItem mniAjout;
	private JMenuItem mniModification;
	private JMenuItem mniSuppression;
	private JButton btnValider;
	private JButton btnRetour;
	private JTabbedPane tabbedPane;
	private Vector<Adherent> lesAdherents = DaoAdherent.getLesAdherents();
	
	//---Panel 'ajout'
	private JLabel lblNom;
	private JLabel lblNom_1;
	private JLabel lblPrenom;
	private JLabel lblPrenom_1;
	private JLabel lblId;
	private JLabel lblId_1;
	private JLabel lblMdp;
	private JLabel lblMdp_1;
	private JTextField txtNom;
	private JTextField txtMdp;
	private JTextField txtId;
	private JTextField txtPrenom;
	
	@SuppressWarnings("unused")
	private JButton btnValiderAjout;
	
	// --Panel 'modif'
	private JTextField txtPrenomModif;
	private JTextField txtNomModif;
	private JTextField txtIdModif;
	private JTextField txtMdpModif;
	private JTextField txtNumAdherent;
	private JLabel lblNumAdherent;
	
	@SuppressWarnings("rawtypes")
	private JComboBox comboModif;
	
	private JButton btnModifier; 
	private JLabel lblSlectionnerUnAdherent;
	
	//---Panel 'suppr'
	@SuppressWarnings("rawtypes")
	private JComboBox comboSuppr;
	
	private JButton btnSupprimer;
	private JLabel lblPrenomSuppr;
	private JTextField txtPrenomSuppr;
	private JLabel lblNomSuppr;
	private JTextField txtNomSuppr;
	private JLabel lblIdSuppr;
	private JTextField txtIdSuppr;
	private JLabel lblMdpSuppr;
	private JTextField txtMdpSuppr;
	private JTextField txtNumAdhASuppr;
	private JLabel lblNumAdhASuppr;
	
	private JButton btnRetourModif;
	private JButton btnRetourSuppr;
	private JPanel panel;
	private JPanel panel_1;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) 
	{
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try
				{
					FenetreGestionMoniteur frame = new FenetreGestionMoniteur();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	@SuppressWarnings({ "deprecation", "rawtypes", "unchecked" })
	public FenetreGestionMoniteur()
	{
		setBounds(100, 100, 488, 327);
		
		// ---- MENU ---
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		// --- MENU FICHIER ---
		mnFichier = new JMenu("Fichier");
		menuBar.add(mnFichier);
		
		mniQuitter = new JMenuItem("Quitter");
		menuBar.add(mnFichier);
		
		mnFichier.add(mniQuitter);
		
		// --- MENU GESTION ---
		mnGestion = new JMenu("Gestion");
		menuBar.add(mnGestion);
		
		mniAjout = new JMenuItem("Ajout");
		mniAjout.addActionListener(this);
		mnGestion.add(mniAjout);
		
		mniModification = new JMenuItem("Modification");
		mnGestion.add(mniModification);
		
		mniSuppression = new JMenuItem("Suppression");
		mnGestion.add(mniSuppression);
		
		
		// -------
		contenu = new JPanel();
		contenu.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contenu);
		contenu.setLayout(new BorderLayout());
				
		JPanel pan2 = new JPanel();
		pan2.setBackground(Color.darkGray);
		
		contenu.add(pan2, BorderLayout.CENTER);
		pan2.setLayout(null);
		
		// --- Creation des onglets ---
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		pan2.add(tabbedPane);
		tabbedPane.setBounds(0, 0, 455, 252);
		
		JPanel panelAjout = new JPanel();
		panelAjout.setBackground(new Color(192, 192, 192));
	
		btnValider = new JButton("Ajouter");
		btnValider.setBounds(360, 190, 80, 20);
		btnValider.addActionListener(this);
		panelAjout.add(btnValider);
		
		btnRetour = new JButton("Retour");
		btnRetour.setBounds(270, 190, 80, 20);
		btnRetour.addActionListener(this);
		panelAjout.add(btnRetour);
				
		tabbedPane.addTab("Ajout", panelAjout);
		panelAjout.setLayout(null);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Renseignements", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 139, 139)));
		panel_1.setBounds(61, 11, 338, 162);
		panelAjout.add(panel_1);
		panel_1.setLayout(null);
		txtNom = new JTextField();
		txtNom.setBounds(164, 22, 80, 20);
		panel_1.add(txtNom);
		
		lblNom = new JLabel("Nom");
		lblNom.setBounds(64, 22, 80, 20);
		panel_1.add(lblNom);
		lblPrenom = new JLabel("Prenom");
		lblPrenom.setBounds(64, 53, 80, 20);
		panel_1.add(lblPrenom);
		txtPrenom = new JTextField();
		txtPrenom.setBounds(164, 53, 80, 20);
		panel_1.add(txtPrenom);
		txtId = new JTextField();
		txtId.setBounds(164, 84, 80, 20);
		panel_1.add(txtId);
		txtMdp = new JTextField();
		txtMdp.setBounds(164, 115, 80, 20);
		panel_1.add(txtMdp);
		lblMdp = new JLabel("Mot de passe");
		lblMdp.setBounds(64, 115, 90, 20);
		panel_1.add(lblMdp);
		lblId = new JLabel("Identifiant");
		lblId.setBounds(64, 84, 80, 20);
		panel_1.add(lblId);
		// END AJOUT
		
		// -- MODIF --
		JPanel panelModif = new JPanel();
		panelModif.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelModif.setBackground(new Color(192, 192, 192));
		panelModif.setLayout(null);
		
		comboModif = new JComboBox(DaoAdherent.getLesAdherents());
		comboModif.addItemListener(this);
		panelModif.add(comboModif);
		comboModif.setBounds(156,11,163,20);
		
		lblNumAdherent = new JLabel("N° adhérent :");
		txtNumAdherent = new JTextField();
		
		lblNumAdherent.setBounds(335,10,80,20);
		panelModif.add(lblNumAdherent);
		txtNumAdherent.setBounds(411,10,45,20);
		panelModif.add(txtNumAdherent);
		
		btnModifier = new JButton("Modifier");
		btnModifier.setBounds(362, 201, 80, 20);
		btnModifier.addActionListener(this);
		panelModif.add(btnModifier);
		
		tabbedPane.addTab("modif", panelModif);
		panelModif.setLayout(null);
		
		lblSlectionnerUnAdherent = new JLabel("S\u00E9lectionner un adh\u00E9rent :");
		lblSlectionnerUnAdherent.setBounds(10, 13, 179, 17);
		panelModif.add(lblSlectionnerUnAdherent);
		
		btnRetourModif = new JButton("Retour");
		btnRetourModif.setBounds(264, 201, 80, 20);
		btnRetourModif.addActionListener(this);
		panelModif.add(btnRetourModif);
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Renseignements", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 139, 139)));
		panel.setBounds(51, 41, 322, 154);
		panelModif.add(panel);
		panel.setLayout(null);
		lblNom_1 = new JLabel("Nom :");
		lblNom_1.setBounds(91, 23, 49, 20);
		panel.add(lblNom_1);
		txtNomModif = new JTextField();
		txtNomModif.setBounds(178, 23, 80, 20);
		panel.add(txtNomModif);
		txtPrenomModif = new JTextField();
		txtPrenomModif.setBounds(178, 54, 80, 20);
		panel.add(txtPrenomModif);
		lblPrenom_1 = new JLabel("Prenom :");
		lblPrenom_1.setBounds(91, 54, 55, 20);
		panel.add(lblPrenom_1);
		lblId_1 = new JLabel("Identifiant :");
		lblId_1.setBounds(91, 92, 68, 20);
		panel.add(lblId_1);
		txtIdModif = new JTextField();
		txtIdModif.setBounds(178, 92, 80, 20);
		panel.add(txtIdModif);
		txtMdpModif = new JTextField();
		txtMdpModif.setBounds(178, 123, 80, 20);
		panel.add(txtMdpModif);
		lblMdp_1 = new JLabel("Mot de passe :");
		lblMdp_1.setBounds(88, 123, 96, 20);
		panel.add(lblMdp_1);
		
		lblNumAdherent.disable(); 
		txtNumAdherent.disable();
		//---END MODIF
		
		// -- SUPPR --
		JPanel panelSuppr = new JPanel();
		panelSuppr.setBackground(new Color(192, 192, 192));
		panelSuppr.setLayout(null);
		
		comboSuppr = new JComboBox(DaoAdherent.getLesAdherents());
		comboSuppr.addItemListener(this);
		panelSuppr.add(comboSuppr);
		comboSuppr.setBounds(148, 11, 150, 20);
		
		lblNomSuppr = new JLabel("Nom");
		txtNomSuppr = new JTextField();
		lblPrenomSuppr = new JLabel("Prenom");
		txtPrenomSuppr = new JTextField();
		lblIdSuppr= new JLabel("Identifiant");
		txtIdSuppr = new JTextField();
		lblMdpSuppr = new JLabel("Mot de passe");
		txtMdpSuppr = new JTextField();
		lblNumAdhASuppr = new JLabel("N° Adhérent :");
		txtNumAdhASuppr = new JTextField();
		
		lblNumAdhASuppr.setBounds(335,10,80,20);
		panelSuppr.add(lblNumAdhASuppr);
		txtNumAdhASuppr.setBounds(411,10,45,20);
		panelSuppr.add(txtNumAdhASuppr);
		
		lblNomSuppr.setBounds(90, 62, 80, 20);
		panelSuppr.add(lblNomSuppr);
		txtNomSuppr.setBounds(180, 62, 80, 20);
		panelSuppr.add(txtNomSuppr);
		
		lblPrenomSuppr.setBounds(90, 93, 80, 20);
		panelSuppr.add(lblPrenomSuppr);
		txtPrenomSuppr.setBounds(180, 93, 80, 20);
		panelSuppr.add(txtPrenomSuppr);
		
		lblIdSuppr.setBounds(90, 124, 80, 20);
		panelSuppr.add(lblIdSuppr);
		txtIdSuppr.setBounds(180, 124, 80, 20);
		panelSuppr.add(txtIdSuppr);
		
		lblMdpSuppr.setBounds(90, 155, 100, 20);
		panelSuppr.add(lblMdpSuppr);
		txtMdpSuppr.setBounds(180, 155, 80, 20);
		panelSuppr.add(txtMdpSuppr);
		
		btnSupprimer = new JButton("Supprimer");
		btnSupprimer.setBounds(335, 190, 100, 20);
		btnSupprimer.addActionListener(this);
		panelSuppr.add(btnSupprimer);
		
		tabbedPane.addTab("Suppression", panelSuppr);
		tabbedPane.addTab("suppr", panelSuppr);
		panelSuppr.setLayout(null);
		
		btnRetourSuppr = new JButton("Retour");
		btnRetourSuppr.addActionListener(this);
		btnRetourSuppr.setBounds(212, 190, 100, 20);
		panelSuppr.add(btnRetourSuppr);
		
		lblNumAdhASuppr.disable();
		txtNumAdhASuppr.disable();
		txtMdpSuppr.disable();
		txtIdSuppr.disable();
		txtPrenomSuppr.disable();
		txtNomSuppr.disable();		
		
		// END SUPPR
}
	
	@SuppressWarnings("static-access")
	@Override
	public void actionPerformed(ActionEvent evt)
	{
		if(evt.getSource() == btnRetour)
			{this.dispose();}
		
		if(evt.getSource() == btnRetourModif)
			{this.dispose();}
		
		if(evt.getSource() == btnRetourSuppr)
			{this.dispose();}	
		
		// OK !
		if(evt.getSource() == btnValider)
		{
			Adherent unAdherent;
			int nb = 0;
			
	        if (this.txtNom.getText().isEmpty() || this.txtPrenom.getText().isEmpty() || this.txtId.getText().isEmpty() || this.txtMdp.getText().isEmpty())
	        { 
	        	JOptionPane.showMessageDialog(null, "Veuillez remplir tous les champs, merci");
	        }
	        else
	        {
	                //type = "Moniteur" car nous n'avons que des moniteurs à ajouter
	        		String type = "Moniteur";
	        		
	           		unAdherent = new Adherent(0,txtNom.getText(), txtPrenom.getText(), txtId.getText(), txtMdp.getText(), type);
	                
					try 
					{
						nb = dao.DaoAdherent.ajouterAdherent(unAdherent);
					} 
					catch (SQLException e)
					{
						e.printStackTrace();
					}
	               
	                if(nb==0)
	                {
	                	JOptionPane.showMessageDialog(null, "L'adhérent existe déjà !" ,"Erreur d'ajout", JOptionPane.ERROR_MESSAGE);
	                }
	                else
	                {
	                	JOptionPane.showMessageDialog(null, "L'adhérent a été ajouté.","Confirmation", JOptionPane.INFORMATION_MESSAGE);
	                    this.lesAdherents.add(unAdherent);
	                    this.dispose();   
	                }
	        }		
		}
		
		// OK !
		if(evt.getSource() == btnModifier)
		{	
			Adherent unAdherent;
			int nb = 0;
			
			if (this.txtNomModif.getText().isEmpty() || this.txtPrenomModif.getText().isEmpty() || this.txtIdModif.getText().isEmpty() || this.txtMdpModif.getText().isEmpty())
	        { 
	        	JOptionPane.showMessageDialog(null, "Veuillez remplir tous les champs, merci");
	        }
	        else
	        {
	        	//type = "Moniteur" car nous n'avons que des moniteurs à ajouter
        		String type = "Moniteur";
        		
           		unAdherent = new Adherent(Integer.parseInt(txtNumAdherent.getText()), txtNomModif.getText(), txtPrenomModif.getText(), txtIdModif.getText(), txtMdpModif.getText(), type);
           		try 
				{
					nb = dao.DaoAdherent.modifierAdherent(unAdherent);
//					System.out.println(nb);
				} 
				catch (SQLException e)
				{
					// TODO Bloc catch généré automatiquement
					e.printStackTrace();
				}
               
                if(nb==0)
                {
                	JOptionPane.showMessageDialog(null, "L'adhérent existe déjà !" ,"Erreur de modification", JOptionPane.ERROR_MESSAGE);
                }
                else
                {
                	JOptionPane.showMessageDialog(null, "L'adhérent a été modifié.","Confirmation", JOptionPane.INFORMATION_MESSAGE);
                	this.lesAdherents.add(unAdherent);  
                }
	        }
			}
		
		if(evt.getSource() == btnSupprimer)
		{
			JOptionPane confirmer = new JOptionPane();
			int confirmation = confirmer.showConfirmDialog(null,"Voulez-vous supprimer votre adhérent ?","Suppression d'un adhérent",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
			
            if(confirmation == 1)
            {
            	JOptionPane.showMessageDialog(null, "L'adhérent n'est pas supprimé",
            			"Information", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                if(confirmation == 0)
                {
					try 
					{
						DaoAdherent.supprimerAdherent(Integer.parseInt(txtNumAdhASuppr.getText()));
					}
					catch (NumberFormatException e)
					{
						e.printStackTrace();
					}
					catch (SQLException e) 
					{
						e.printStackTrace();
					}
						JOptionPane.showMessageDialog(null, "L'adhérent a été supprimé !","Confirmation de supperssion", JOptionPane.INFORMATION_MESSAGE);
                }
          	}
		}
	}
	
	public void itemStateChanged(ItemEvent itemEvt)
	{
		if(itemEvt.getSource() == comboModif)
		{
			Adherent unAdherent = (Adherent) comboModif.getSelectedItem();
		
			txtNomModif.setText(unAdherent.getNom());
			txtPrenomModif.setText(unAdherent.getPrenom());
			txtIdModif.setText(unAdherent.getLogin());
			txtMdpModif.setText(unAdherent.getPassword());
			txtNumAdherent.setText(String.valueOf(unAdherent.getNumAdherent()));
		}
		
		if(itemEvt.getSource() == comboSuppr)
		{
			Adherent unAdherent = (Adherent) comboSuppr.getSelectedItem();
			
			txtNomSuppr.setText(unAdherent.getNom());
			txtPrenomSuppr.setText(unAdherent.getPrenom());
			txtIdSuppr.setText(unAdherent.getLogin());
			txtMdpSuppr.setText(unAdherent.getPassword());
			txtNumAdhASuppr.setText(String.valueOf(unAdherent.getNumAdherent()));
		}
		
	}	
}